package com.shop.shoppingmall.controller;

import com.shop.shoppingmall.dao.ShUserDao;
import com.shop.shoppingmall.entity.ShFollowProduct;
import com.shop.shoppingmall.entity.ShUser;
import com.shop.shoppingmall.service.ShFollowProductService;
import com.shop.shoppingmall.util.CommonConstant;
import com.shop.shoppingmall.util.JwtUtil;
import com.shop.shoppingmall.util.api.vo.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 关注商品(ShFollowProduct)表控制层
 *
 * @author makejava
 * @since 2020-09-09 10:05:51
 */
@RestController
@RequestMapping("shFollowProduct")
@CrossOrigin
public class ShFollowProductController {
    /**
     * 服务对象
     */
    @Resource
    private ShFollowProductService shFollowProductService;
    @Resource
    private ShUserDao shUserDao;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ShFollowProduct selectOne(String id) {
        return this.shFollowProductService.queryById(id);
    }

    /**
     * 对商品进行关注和取消关注操作
     * @param map
     * @return
     */
    @PostMapping("/shFollowProduct")
    public Result shFollowProduct(@RequestBody Map map , HttpServletRequest request){
        String productId = (String) map.get("productId");
        String isKeep = (String) map.get("isKeep");
        if(isKeep.equals("1")){
            return shFollowProductService.shFollowProduct(request,productId);
        }else {
            return shFollowProductService.deleteProduct(request,productId);
        }
    }

    @PostMapping("/selectProduct")
    public Result selectProduct( HttpServletRequest request){
        String nameByToken = JwtUtil.getUserNameByToken(request);
        ShUser shUser = shUserDao.selectLoginName(nameByToken);
        if(shUser == null){
            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
        }
        return shFollowProductService.selectProduct(shUser.getId());
    }

}